package com.bris.niosocket.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bris.niosocket.message.impl.RequestMessage;

/**
 * Created with IntelliJ IDEA.
 * User: Dempe
 * Date: 2015/12/10
 * Time: 17:36
 * To change this template use File | Settings | File Templates.
 */
public class DispatcherHandler extends ChannelInboundHandlerAdapter {

    public static final Logger log = LoggerFactory.getLogger(DispatcherHandler.class);
    private final static ExecutorService workerThreadService = Executors.newFixedThreadPool(50,new DefaultThreadFactory("workerThread"));

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof RequestMessage) {
            workerThreadService.submit(new TaskWorker(ctx, (RequestMessage)msg));
        }


    }
    
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
    	log.error("Netty IO异常!",cause);
    	ctx.close();
    }


}
